<?php

namespace mbilling\BillingBundle\Other;

class Date
{
    static public function convertDate($oldfmt, $date, $newfmt="Y-m-d")
    {
        $d=strpos($oldfmt,"d");
        $m=strpos($oldfmt,"m");
        $y=strpos($oldfmt,"y");
        if (!is_integer($d) || !is_integer($m) || !is_integer($y))
        {
            trigger_error ('Invalid input format', E_USER_ERROR);
        }
        else
        {
            $d++; $m++; $y++;
            preg_match("/(\d+).(\d+).(\d+)/", $date, $regs);
            if (isset($regs[$m]) && isset($regs[$y]) && isset($regs[$d]))
            {
                if ($regs[$y] >=0 && $regs[$y] <=69)
                {
                    $regs[$y]+=2000;
                }
                if ($regs[$y] >=70 && $regs[$y] <=99)
                {
                    $regs[$y]+=1900;
                }
                if ( $regs[$y] > 1000 && checkdate($regs[$m],$regs[$d],$regs[$y]))
                {
                    return date ($newfmt, mktime (0,0,0,$regs[$m],$regs[$d],$regs[$y]));
                }
            }
            trigger_error ("Invalid Date $date", E_USER_WARNING);
        }
    }
}
?>